// 专门处理请求,获取图片信息
export const request = (params) => {
  //定义公共图片路径
  const baseUrl = "http://q92vb3yye.bkt.clouddn.com/";

  return new Promise((resolve, reject) => {
    wx.request({
      ...params,
      url: baseUrl + params.url,
      success: (result) => { 
        resolve(result);
      },
      fail: (error) => {
        reject(error);
      }
    })
  })
}

// 专门处理    获取用户信息的请求
export const requestGetUserMsg = (params) => {
  return new Promise((resolve, reject) => {
    wx.request({
      ...params,
      url: "http://127.0.0.1:80"+params.url,
      success: (result) => {
        resolve(result);
      },
      fail: (error) => {
        reject(error);
      }
    })
  })
}

// 跳转页面
export const navigateTo = (params) => {
  return new Promise((resolve, reject) => {
    // console.log(params)
    wx.navigateTo({
      ...params,
      url: params.url,
      success: (result) => {
        resolve(result);
      },
      fail: (error) => {
        reject(error);
      }
    })

  })
}

//   展示模态框
export const showModal = (params) => {
  return new Promise((resolve, reject) => {
    wx.showModal({
      ...params,
      title: params.title,
      confirmText: "确定",
      cancelText: "取消",
      success: (res) => {
        resolve(res);
      },
      fail: (error) => {
        reject(error);
      }
    })

  })
}


// 选择地址
export const getLocation = () => {
  return new Promise((resolve, reject) => {
    wx.getLocation({
      type: 'wgs84',
      altitude: true,
      success: (res) => {
        resolve(res);
      },
      fail: (error) => {
        reject(error);
      }
    });

  })
}

//获取用户需要的权限

// 选择地址
export const getSetting = () => {
  return new Promise((resolve, reject) => {
    wx.getSetting({
      success: (result) => {
        // console.log("调用成功");
        resolve(result);
      },
      fail: (error) => {
        // console.log("失败");
        reject(error);
      }
    });

  })
}
// openSetting
export const openSetting = () => {
  return new Promise((resolve, reject) => {
    wx.openSetting({
      success: (result) => {
        resolve(result);
      },
      fail: (err) => {
        reject(err);
      },

    });

  })
}

// chooseLocation  根据精度和纬度选择位置
// latitude  纬度
// longitude 精度
export const chooseLocation = (params) => {
  return new Promise((resolve, reject) => {
    wx.chooseLocation({
      latitude: params.latitude,
      longitude: params.longitude,
      success: (res) => {
        resolve(res);
      },
      fail: (err) => {
        reject(err);
      }
    });

  })
}
// wx.login  获取用户的code值
export const login = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      timeout: 10000,
      success: (result) => {
        // console.log(result.code);
        resolve(result);
      },
      fail: (res) => {
        reject(res);
      },
    });
  })
}


// wx.request 请求开发者服务器
export const loginRequest = (params) => {
  return new Promise((resolve, reject) => {
    var loginRequest = wx.request({
      ...params,
      url:'http://127.0.0.1:80/getByCode?js_code='+params.js_code,
      method: 'GET',
      success: (result) => {
        // console.log(result);
        resolve(result);
      },
      fail: (error) => {
        reject(error);
      },
    });
  })
}
